package com.sunday.fangdaifu.activity.task;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.reflect.TypeToken;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.adapter.HistoryAdapter;
import com.sunday.fangdaifu.common.ApiClient;
import com.sunday.fangdaifu.common.ApiServiceImpl;
import com.sunday.fangdaifu.model.HistoryTaskList;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/7/5.
 */

public class HistoryTaskListActivity extends BaseActivity implements ApiServiceImpl.HttpResponseInterface {
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private HistoryAdapter adapter;
    private LinearLayoutManager layoutManager;
    private List<HistoryTaskList> historyTaskListList =new ArrayList<>();
    private int pageNo=1;
    private int pageSize=10;
    private int id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);
        id=getIntent().getIntExtra("id",0);
        init();
    }
    private void init(){
        adapter=new HistoryAdapter(mContext);
        layoutManager=new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).drawable(R.drawable.shape_divider).build());
        recyclerView.setAdapter(adapter);
        showLoadingDialog();
        ApiClient.getApiService().historyReportList(id,pageNo,pageSize, this, new TypeToken<ResultDO<List<HistoryTaskList>>>() {
        }.getType());

    }

    @Override
    public void onResponce(String key, Object result) {
        if (isFinish){
            return;
        }
        dissMissDialog();
        ResultDO<List<HistoryTaskList>> resultDO = (ResultDO<List<HistoryTaskList>>) result;
        if (resultDO.getCode() == 0) {
            List<HistoryTaskList> historyTaskLists =resultDO.getResult();
            if(pageNo==1){
                historyTaskListList.clear();
            }
            historyTaskListList.addAll(historyTaskLists);
            adapter.setData(historyTaskListList);
            adapter.notifyDataSetChanged();
        } else {
            ToastUtils.showToast(mContext,resultDO.getMessage()+"");
        }
    }

    @Override
    public void onFailure() {

    }
}
